package com.example.blogsystem.controller;

import com.example.blogsystem.constant.Constants;
import com.example.blogsystem.model.Blog;
import com.example.blogsystem.model.Result;
import com.example.blogsystem.model.User;
import com.example.blogsystem.service.BlogService;
import com.example.blogsystem.service.UserService;
import com.example.blogsystem.utils.JwtUtils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/blog")
@RestController
@Slf4j
public class BlogController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    //全量查询博客列表
    @RequestMapping("/getBlogList")
    public List<Blog> getBlogList(){
        return blogService.queryAllBlogs();
    }

    //根据博客ID查询
    @RequestMapping("/getBlogById")
    public Blog getBlogById(Integer blogId, HttpServletRequest request){
        //校验参数的合法性
        if(blogId == null || blogId <= 0){
            return null;
        }
        //查询文章
        Blog blog = blogService.queryBlogById(blogId);
        //判断当前文章是不是登录者
        Integer loginUserId = userService.getLoginUser(request);
        if(loginUserId != null && loginUserId.equals(blog.getUserId())){
            blog.setLoginUser(1);
        }
        return blog;
    }

    //新增博客
    @RequestMapping("/addBlog")
    public Result addBlog(Blog blog, HttpServletRequest request){
        //校验参数
        if(blog == null){
            return Result.fail("用户输入有误");
        }
        if(!StringUtils.hasLength(blog.getTitle())){
            return Result.fail("文章标题为空");
        }
        if(!StringUtils.hasLength(blog.getContent())){
            return Result.fail("文章内容为空");
        }
        if(!StringUtils.hasLength(blog.getSummary())){
            return Result.fail("摘要内容为空");
        }
        if(blog.getSummary().length() > 200){
            return Result.fail("摘要字数超过200");
        }
        //获取当前上传者的信息
        Integer loginUserId = userService.getLoginUser(request);
        if(loginUserId == null){
            return Result.unLogin();
        }
        blog.setUserId(loginUserId);
        //上传博客
        blogService.insertBlog(blog);

        return Result.success("");
    }

    //更新博客
    @RequestMapping("/updateBlog")
    public Result updateBlog(Blog newBlog, HttpServletRequest request){
        //校验参数
        if(newBlog.getId() == null || newBlog.getId() <= 0){
            return Result.fail("非法输入");
        }
        //判断是否非法修改
        Integer loginUserId = userService.getLoginUser(request);
        if(loginUserId == null || !loginUserId.equals(newBlog.getUserId())){
            return Result.fail("登录用户不一致，非法修改！");
        }
        //进行修改
        blogService.updateBlog(newBlog);

        return Result.success("");
    }

    //删除博客
    @RequestMapping("/deleteBlog")
    public Result deleteBlog(Integer blogId, HttpServletRequest request) {
        //校验参数
        if (blogId == null || blogId <= 0) {
            return Result.fail("非法输入");
        }
        //判断是否非法删除
        Integer loginUserId = userService.getLoginUser(request);
        Blog blog = blogService.queryBlogById(blogId);
        if (loginUserId == null || !loginUserId.equals(blog.getUserId())) {
            return Result.fail("删除失败");
        }

        //删除博客
        blogService.deleteBlog(blog.getId());
        return Result.success("");
    }
}
